<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="简称" prop="abbreviation">
        <el-input v-model="dataForm.abbreviation" placeholder="简称"></el-input>
      </el-form-item>
      <el-form-item label="父ID" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder="父ID"></el-input>
      </el-form-item>
      <el-form-item label="部门类型">
        <el-radio v-model="dataForm.orgType" label="1" >分公司</el-radio>
        <el-radio v-model="dataForm.orgType" label="2">一级转运中心</el-radio>
        <el-radio v-model="dataForm.orgType" label="3">二级转运中心</el-radio>
        <el-radio v-model="dataForm.orgType" label="4">网点</el-radio>
      </el-form-item>

      <el-form-item label="省市区">
        <el-row>
          <el-select v-model="provinceValue" placeholder="请选择" @change="selectProvimce">
            <el-option
              v-for="(item,index) of provincearr"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>

          <el-select v-model="cityValue" placeholder="请选择" @change="selectcity">
            <el-option
              v-for="(item,index) of cityarr"
              :key="index"
              :label="item.name"
              :value="item.id">

            </el-option>
          </el-select>

          <el-select v-model="regionValue" placeholder="请选择">
            <el-option
              v-for="(item,index) of regionarr"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-row>
      </el-form-item>

      <el-form-item label="描述" prop="describe">
        <el-input v-model="dataForm.describe" placeholder="描述"></el-input>
      </el-form-item>

      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
      </el-form-item>

      <el-form-item label="经度" prop="longitude">
        <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
      </el-form-item>

      <el-form-item label="机构作业半径" prop="longitude">
        <el-input v-model="dataForm.coreOrgRadius" placeholder="机构作业半径"></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="danger" round @click="visible = false">取消</el-button>
      <el-button type="success" round @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import {selectsheng} from '@/api/user'

export default {

  data() {
    return {
      //省
      provincearr: [],
      //市
      cityarr: [],
      //区县
      regionarr: [],

      provinceValue: "",

      cityValue: "",

      regionValue: "",



      visible: false,
      dataForm: {
        id: 0,
        name: '',
        abbreviation: '',
        parentId: '',
        orgType: '1',
        provinceId: '',
        cityId: '',
        countyId: '',
        address: '',
        contractNumber: '',
        managerId: '',
        treePath: '',
        sortValue: '',
        status: '',
        describe: '',
        latitude: '',
        longitude: '',
        businessHours: '',
        coreOrgRadius: 0
      },
      dataRule: {
        name: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        abbreviation: [
          {required: true, message: '简称不能为空', trigger: 'blur'}
        ],
        parentId: [
          {required: true, message: '父ID不能为空', trigger: 'blur'}
        ],
        orgType: [
          {required: true, message: '部门类型 1为分公司，2为一级转运中心 3为二级转运中心 4为网点不能为空', trigger: 'blur'}
        ],
        provinceId: [
          {required: true, message: '省不能为空', trigger: 'blur'}
        ],
        cityId: [
          {required: true, message: '市不能为空', trigger: 'blur'}
        ],
        countyId: [
          {required: true, message: '区不能为空', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '地址不能为空', trigger: 'blur'}
        ],
        contractNumber: [
          {required: true, message: '联系电话不能为空', trigger: 'blur'}
        ],
        managerId: [
          {required: true, message: '负责人id不能为空', trigger: 'blur'}
        ],
        treePath: [
          {required: true, message: '树结构不能为空', trigger: 'blur'}
        ],
        sortValue: [
          {required: true, message: '排序不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '状态不能为空', trigger: 'blur'}
        ],
        describe: [
          {required: true, message: '描述不能为空', trigger: 'blur'}
        ],
        latitude: [
          {required: true, message: '纬度不能为空', trigger: 'blur'}
        ],
        longitude: [
          {required: true, message: '经度不能为空', trigger: 'blur'}
        ],
        businessHours: [
          {required: true, message: '营业时间不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0

      var dataid = this.dataForm.id

      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/auth/coreorg/info/` + dataid),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.dataForm.name = data.coreOrg.name
            this.dataForm.abbreviation = data.coreOrg.abbreviation
            this.dataForm.parentId = data.coreOrg.parentId
            this.dataForm.orgType = data.coreOrg.orgType

            this.dataForm.provinceId = data.coreOrg.provinceId
            this.dataForm.cityId = data.coreOrg.cityId
            this.dataForm.countyId = data.coreOrg.countyId

            this.dataForm.address = data.coreOrg.address
            this.dataForm.contractNumber = data.coreOrg.contractNumber
            this.dataForm.managerId = data.coreOrg.managerId
            this.dataForm.treePath = data.coreOrg.treePath
            this.dataForm.sortValue = data.coreOrg.sortValue
            this.dataForm.status = data.coreOrg.status
            this.dataForm.describe = data.coreOrg.describe
            this.dataForm.latitude = data.coreOrg.latitude
            this.dataForm.longitude = data.coreOrg.longitude
            this.dataForm.businessHours = data.coreOrg.businessHours
            this.dataForm.coreOrgRadius = data.coreOrg.coreOrgRadius
          })
        }
      })
    },

    // 表单提交
    dataFormSubmit() {
      console.log()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/auth/coreorg/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'abbreviation': this.dataForm.abbreviation,
              'parentId': this.dataForm.parentId,
              'orgType': this.dataForm.orgType,

              'provinceId': this.provinceValue,
              'cityId': this.cityValue,
              'countyId': this.regionValue,

              'address': this.dataForm.address,
              'contractNumber': this.dataForm.contractNumber,
              'managerId': this.dataForm.managerId,
              'treePath': this.dataForm.treePath,
              'sortValue': this.dataForm.sortValue,
              'status': this.dataForm.status,
              'describe': this.dataForm.describe,
              'latitude': this.dataForm.latitude,
              'longitude': this.dataForm.longitude,
              'businessHours': this.dataForm.businessHours,
              'coreOrgRadius': this.dataForm.coreOrgRadius
            })
          }).then(({data}) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    },
    //获取省份
    selectProvimce1() {
      selectsheng(0).then(({data}) => {
        this.provincearr = data;
      })
    },

    //获取市
    selectProvimce() {
      var provinceValueid = this.provinceValue;
      selectsheng(provinceValueid).then(({data}) => {
        this.cityarr = data;

      })
    },
    //获取区县
    selectcity() {
      var cityValueid = this.cityValue;
      selectsheng(cityValueid).then(({data}) => {
        this.regionarr = data;

      })
    },
  },
  mounted() {
    this.selectProvimce1()
  }
}
</script>
<style>
.container {
  width: 800px;
  height: 400px;
  overflow: hidden;
}

.box {
  width: 800px;
  height: 50px;
  line-height: 50px;
  background: red;
  color: #fff;
}
</style>
